#include "drv_iic.h"
#include "stm32g4xx_hal.h"
#include "util.h"
#include "FreeRTOS.h"

#define I2C_ADDRESS (0x78)

I2C_HandleTypeDef hi2c3;
DMA_HandleTypeDef hdma_i2c3_tx;
DMA_HandleTypeDef hdma_i2c3_rx;

void iicInit(void)
{
    __HAL_RCC_DMAMUX1_CLK_ENABLE();
    __HAL_RCC_DMA2_CLK_ENABLE();

    HAL_NVIC_SetPriority(DMA2_Channel1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(DMA2_Channel1_IRQn);
    HAL_NVIC_SetPriority(DMA2_Channel2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(DMA2_Channel2_IRQn);

    hi2c3.Instance = I2C3;
    hi2c3.Init.Timing = 0x40621236;
    hi2c3.Init.OwnAddress1 = 0;
    hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c3.Init.OwnAddress2 = 0;
    hi2c3.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    HAL_I2C_Init(&hi2c3);

    HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE);
    HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0);
}

void iicDeinit(void)
{
    HAL_NVIC_DisableIRQ(DMA1_Channel2_IRQn);
    HAL_NVIC_DisableIRQ(I2C3_ER_IRQn);
    HAL_I2C_DeInit(&hi2c3);
}

void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
    if(hi2c->Instance==I2C3) {
        PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C3;
        PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
        HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);

        __HAL_RCC_GPIOC_CLK_ENABLE();

        GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF8_I2C3;
        HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

        __HAL_RCC_I2C3_CLK_ENABLE();

        hdma_i2c3_tx.Instance = DMA2_Channel1;
        hdma_i2c3_tx.Init.Request = DMA_REQUEST_I2C3_TX;
        hdma_i2c3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
        hdma_i2c3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_i2c3_tx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_i2c3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_i2c3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
        hdma_i2c3_tx.Init.Mode = DMA_NORMAL;
        hdma_i2c3_tx.Init.Priority = DMA_PRIORITY_LOW;
        HAL_DMA_Init(&hdma_i2c3_tx);

        __HAL_LINKDMA(hi2c,hdmatx,hdma_i2c3_tx);

        hdma_i2c3_rx.Instance = DMA2_Channel2;
        hdma_i2c3_rx.Init.Request = DMA_REQUEST_I2C3_RX;
        hdma_i2c3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
        hdma_i2c3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_i2c3_rx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_i2c3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_i2c3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
        hdma_i2c3_rx.Init.Mode = DMA_NORMAL;
        hdma_i2c3_rx.Init.Priority = DMA_PRIORITY_HIGH;
        HAL_DMA_Init(&hdma_i2c3_rx);

        __HAL_LINKDMA(hi2c,hdmarx,hdma_i2c3_rx);

        HAL_NVIC_SetPriority(I2C3_ER_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(I2C3_ER_IRQn);
        HAL_NVIC_SetPriority(I2C3_EV_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(I2C3_EV_IRQn);
    }
}

void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
{
    if(hi2c->Instance==I2C3) {
        __HAL_RCC_I2C3_CLK_DISABLE();

        HAL_GPIO_DeInit(GPIOC, GPIO_PIN_8);
        HAL_GPIO_DeInit(GPIOC, GPIO_PIN_9);

        HAL_DMA_DeInit(hi2c->hdmatx);
        HAL_DMA_DeInit(hi2c->hdmarx);
        HAL_NVIC_DisableIRQ(I2C3_ER_IRQn);
    }
}

void writeIicByte(uint8_t reg, uint8_t data)
{
    HAL_I2C_Mem_Write_DMA(&hi2c3, I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, &data, 1);
    while(HAL_I2C_GetState(&hi2c3) != HAL_I2C_STATE_READY);
}

void writeIicBytes(uint8_t reg, uint8_t * pdata, uint32_t len)
{
    HAL_I2C_Mem_Write_DMA(&hi2c3, I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, pdata, len);
    while(HAL_I2C_GetState(&hi2c3) != HAL_I2C_STATE_READY);
}

void DMA2_Channel1_IRQHandler(void)
{
    HAL_DMA_IRQHandler(&hdma_i2c3_tx);
}

void DMA2_Channel2_IRQHandler(void)
{
    HAL_DMA_IRQHandler(&hdma_i2c3_rx);
}

void I2C3_ER_IRQHandler(void)
{
    HAL_I2C_ER_IRQHandler(&hi2c3);
}

void I2C3_EV_IRQHandler(void)
{
    HAL_I2C_EV_IRQHandler(&hi2c3);
}